home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / misc / pdflib / p_text.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  4KB  |  199 lines

  1. /* p_text.c
  2.  * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
  3.  *
  4.  * PDFlib text routines
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. #include "p_intern.h"
  10.  
  11. /* Auxiliary routines */
  12.  
  13. void
  14. pdf_begin_text(PDF *p)
  15. {
  16.     if (p->contents == c_text)
  17.     return;
  18.  
  19.     p->res.procset    |= Text;
  20.     p->contents        = c_text;
  21.  
  22.     (void) fputs( "BT\n", p->fp);
  23. }
  24.  
  25. void
  26. pdf_end_text(PDF *p)
  27. {
  28.     if (p->contents != c_text)
  29.     return;
  30.  
  31.     p->contents    = c_stream;
  32.  
  33.     (void) fputs("ET\n", p->fp);
  34. }
  35.  
  36. /* This function doesn't work with null characters! */
  37.  
  38. void
  39. pdf_quote_string(PDF *p, char *string)
  40. {
  41.     unsigned char *s;
  42.  
  43.     fputc('(', p->fp);
  44.  
  45.     for (s = (unsigned char *) string; *s; s++) {
  46.         /* expand tabs */
  47.         if (*s == '\t') {
  48.             fputs("    ", p->fp);
  49.         } else
  50.         if (p->info->binary_mode == false && (*s < 0x20 || *s > 0x7f)) {
  51.             fprintf(p->fp, "\\%03o", *s);
  52.         } else {
  53.             if (*s == '(' || *s == ')' || *s == '\\')
  54.             fputc('\\', p->fp);
  55.             fputc(*s, p->fp);
  56.         }
  57.     }
  58.     fputc(')', p->fp);
  59. }
  60.  
  61. void
  62. pdf_put_fonts(PDF *p)
  63. {
  64.     pdf_item    *item;
  65.     int        index = 0;
  66.  
  67.     /* output pending font objects */
  68.     for (item = p->res.font; item != (pdf_item *) NULL; item = item->next) 
  69.     {
  70.     pdf_put_t1font(p, item->basename, item->obj_id, index++, true);
  71.     }
  72. }
  73.  
  74. /* Section 8.7.3 Text state operators */
  75.  
  76. void
  77. PDF_set_font(PDF *p, char *fontname, float fontsize, PDF_encoding enc)
  78. {
  79.     pdf_item    *item;
  80.  
  81.     item = pdf_add_res_font(p, fontname);
  82.  
  83.     pdf_begin_text(p);
  84.     (void) fprintf(p->fp, "%s %s Tf\n", item->name, pdf_float(fontsize));
  85.     PDF_set_leading(p, fontsize);
  86.  
  87.     switch (p->current_font->encoding) {
  88.     case builtin:
  89.     case winansi:
  90.     case macroman:
  91.         break;
  92.  
  93.     default:
  94.         pdf_error(p, PDF_WARN, "Bogus encoding for font %s", fontname);
  95.         enc = builtin;
  96.     }
  97.  
  98.     pdf_read_afm(p, fontname, enc);
  99.     p->current_font->encoding = enc;
  100.     p->current_font->size = fontsize;
  101. }
  102.  
  103. void
  104. PDF_set_leading(PDF *p, float l)
  105. {
  106.     pdf_begin_text(p);
  107.     (void) fprintf(p->fp, "%s TL\n", pdf_float(l));
  108. }
  109.  
  110. void
  111. PDF_set_text_rendering(PDF *p, byte mode)
  112. {
  113. #define LAST_MODE    7
  114.     if (mode > LAST_MODE) {
  115.         pdf_error(p, PDF_WARN, "Bogus text rendering mode");
  116.         return;
  117.     }
  118.         
  119.     pdf_begin_text(p);
  120.     (void) fprintf(p->fp, "%d Tr\n", mode);
  121. #undef LAST_MODE
  122. }
  123.  
  124. void
  125. PDF_set_text_rise(PDF *p, float rise)
  126. {
  127.     pdf_begin_text(p);
  128.     (void) fprintf(p->fp, "%s Ts\n", pdf_float(rise));
  129. }
  130.  
  131. void
  132. PDF_set_horiz_scaling(PDF *p, float scale)
  133. {
  134.     pdf_begin_text(p);
  135.     (void) fprintf(p->fp, "%s Tz\n", pdf_float(scale));
  136. }
  137.  
  138. /* Section 8.7.4 Text positioning operators */
  139.  
  140. void
  141. PDF_set_text_matrix(PDF *p, PDF_matrix m)
  142. {
  143.     pdf_begin_text(p);
  144.     (void) fprintf(p->fp,"%s %s %s %s %s %s Tm\n",
  145.           pdf_float(m.a), pdf_float(m.b), pdf_float(m.c),
  146.           pdf_float(m.d), pdf_float(m.e), pdf_float(m.f));
  147. }
  148.  
  149. void
  150. PDF_set_text_pos(PDF *p, float x, float y)
  151. {
  152.     pdf_begin_text(p);
  153.     (void) fprintf(p->fp,"1 0 0 1 %s %s Tm\n", pdf_float(x), pdf_float(y));
  154. }
  155.  
  156. /* Section 8.7.6 Text string operators */
  157.  
  158. void
  159. PDF_show(PDF *p, char *text)
  160. {
  161.     pdf_begin_text(p);
  162.     pdf_quote_string(p, text);
  163.     (void) fputs("Tj\n", p->fp);
  164. }
  165.  
  166. void
  167. PDF_continue_text(PDF *p, char *text)
  168. {
  169.     pdf_begin_text(p);
  170.     pdf_quote_string(p, text);
  171.     (void) fputs("'\n", p->fp);
  172. }
  173.  
  174. void
  175. PDF_show_xy(PDF *p, char *text, float x, float y)
  176. {
  177.     pdf_begin_text(p);
  178.  
  179.     (void) fprintf(p->fp,"1 0 0 1 %s %s Tm\n", pdf_float(x), pdf_float(y));
  180.     pdf_quote_string(p, text);
  181.     (void) fputs("Tj\n", p->fp);
  182. }
  183.  
  184. /* character spacing for justified lines */
  185. void
  186. PDF_set_char_spacing(PDF *p, float spacing)
  187. {
  188.     pdf_begin_text(p);
  189.     (void) fprintf(p->fp,"%s Tc\n", pdf_float(spacing));
  190. }
  191.  
  192. /* word spacing for justified lines */
  193. void
  194. PDF_set_word_spacing(PDF *p, float spacing)
  195. {
  196.     pdf_begin_text(p);
  197.     (void) fprintf(p->fp,"%s Tw\n", pdf_float(spacing));
  198. }
  199.